package com.bootdo.clouddo.utils.http;

import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.bootdo.clouddo.utils.StringUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;

public class JuejinUtils {
	
	/**
     * 向目的URL发送post请求
     * @param url       目的url
     * @param params    发送的参数
     * @return  ResultVO
     */
	public static JSONObject sendPostRequest(String url, Map<String, String> params){
    	try {
    		Thread.sleep(666);
		} catch (Exception e) {
		}
        RestTemplate client = new RestTemplate();
        HttpMethod method = HttpMethod.POST;
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        //将请求头部和参数合成一个请求
        HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(params, headers);
        
        //执行HTTP请求，将返回的结构使用ResultVO类格式化
        ResponseEntity<JSONObject> response = client.exchange(url, method, requestEntity, JSONObject.class);

        return response.getBody();
    }
	
	/**
	 * 向目的URL发送get请求
	 * @param url       目的url
	 * @param params    发送的参数
	 * @return  ResultVO
	 */
	public static JSONObject getRequest(String url, Map<String, String> params){
		try {
			Thread.sleep(666);
		} catch (Exception e) {
		}
		SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
		requestFactory.setConnectTimeout(3000);// 设置超时
		requestFactory.setReadTimeout(3000);
		RestTemplate client = new RestTemplate(requestFactory);
		return client.getForObject(url + "&src={src}&uid={uid}&device_id={device_id}&token={token}", JSONObject.class,  params);
	}

	public static JSONObject getRequestForJSONObject(String url, Map<String, String> params){
		try {
			Thread.sleep(666);
		} catch (Exception e) {
		}
		SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
		requestFactory.setConnectTimeout(3000);// 设置超时
		requestFactory.setReadTimeout(3000);
		RestTemplate client = new RestTemplate(requestFactory);
		return client.getForObject(url, JSONObject.class,  params);
	}

	/**
	 * 向目的URL发送get请求
	 * @param url       目的url
	 * @param params    发送的参数
	 * @return  ResultVO
	 */
	public static JSONArray getRequestForJSONArray(String url, Map<String, String> params){
		try {
			Thread.sleep(66);
		} catch (Exception e) {
		}
		SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
		requestFactory.setConnectTimeout(3000);// 设置超时
		requestFactory.setReadTimeout(3000);
		RestTemplate client = new RestTemplate(requestFactory);
		return client.getForObject(url, JSONArray.class,  params);
	}
	
	/**
     * 向目的URL发送get请求
     * @param url       目的url
     * @param params    发送的参数
     * @return  ResultVO
     */
	public static String getStrRequest(String url, Map<String, String> params){
    	try {
    		Thread.sleep(666);
		} catch (Exception e) {
		}
    	SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    	requestFactory.setConnectTimeout(3000);// 设置超时
    	requestFactory.setReadTimeout(3000);
        RestTemplate client = new RestTemplate(requestFactory);
        return client.getForObject(url + "&src={src}&uid={uid}&device_id={device_id}&token={token}", String.class,  params);
    }
	
	/**
     * 向目的URL发送post请求
     * @param url       目的url
     * @param params    发送的参数
     * @return  ResultVO
     */
	public static JSONObject sendPostRequest(String url, Map<String, String> params, MediaType contentType){
    	try {
    		Thread.sleep(666);
		} catch (Exception e) {
		}
        RestTemplate client = new RestTemplate();
        HttpEntity<Map<String, String>> requestEntity = null;
        if(contentType != null) {
        	HttpHeaders headers = new HttpHeaders();
        	headers.setContentType(contentType);
        	//将请求头部和参数合成一个请求
        	requestEntity = new HttpEntity<>(params, headers);
        }else {
        	requestEntity = new HttpEntity<>(params);
        }
        
        //执行HTTP请求，将返回的结构使用ResultVO类格式化
        return client.postForObject(url, requestEntity, JSONObject.class);
    }

	public static String getNewImageHost(String url){
		if(StringUtils.isNotEmpty(url))
			return url.replaceAll("https://user-gold-cdn.xitu.io",
					"https://bukaa.cn/api-cms/file/getJuejinImg");
		return url;
	}

}
